package com.augurit.gzcgwsp.smartformCsdlwj.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.gzcgwsp.smartformCsdlwj.dao.YwHwZxqksmDao;
import com.augurit.gzcgwsp.smartformCsdlwj.entity.YwHwZxqksm;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwHwZxqksmService;
import com.augurit.gzcgwsp.smartformCsdlwj.web.form.YwHwZxqksmForm;

/*
 * 陆域生活垃圾清扫车辆情况service实现类
 */
@Service
@Transactional
public class YwHwZxqksmServiceImpl  implements IYwHwZxqksmService{
	@Autowired
	private YwHwZxqksmDao ywHwZxqksmDao;
	
	/**
	 * 获取form
	 */
	public YwHwZxqksmForm getForm(Long Id) {
		YwHwZxqksm entity=ywHwZxqksmDao.get(Id);
		YwHwZxqksmForm form=new DozerBeanMapper().map(entity,YwHwZxqksmForm.class);
		return form;
	}

	public List<YwHwZxqksmForm> getListByQyId(Long qyId) {
		List<YwHwZxqksm> list=this.getListByQyid(qyId);
		List<YwHwZxqksmForm> forms=new ArrayList<YwHwZxqksmForm>();
		for(YwHwZxqksm entity:list){
			YwHwZxqksmForm form=new DozerBeanMapper().map(entity,YwHwZxqksmForm.class);
			forms.add(form);
		}
		return forms;
	}

	/**
	 * 根据ywid查询数据
	 * @param QYId
	 * @return
	 */
	private List<YwHwZxqksm> getListByQyid(Long qyId){
		String sql=" from "+YwHwZxqksm.class.getName()+" where  qyid="+qyId;
		List<YwHwZxqksm> list=this.ywHwZxqksmDao.find(sql);
		return list;
	}


	public void SaveOrUpdate(YwHwZxqksmForm form) {
		YwHwZxqksm entity=new DozerBeanMapper().map(form,YwHwZxqksm.class);
		this.ywHwZxqksmDao.save(entity);
	}

	public void SaveOrUpdate(YwHwZxqksm entity) {
		this.ywHwZxqksmDao.save(entity);
	}

	public void parseYwHwZxqksm(HSSFSheet hssfsheet,Long qyid,Long ywid, String templateCode){
		String hql = "from YwHwZxqksm t where t.ywid = '"+ywid+"' and t.tplcode= '"+templateCode+"'";
		List<YwHwZxqksm> list = new ArrayList<YwHwZxqksm>();
		list = this.ywHwZxqksmDao.find(hql, list);
		for(int i=0; i<list.size();i++){
			this.ywHwZxqksmDao.delete(list.get(i));
		}
		try {
			
			HSSFRow hssfrow = hssfsheet.getRow(0);// 第一行
			String hbaqqksm ="";
			String sczyqksm ="";
			String fwzlqksm ="";
			String ylqksm ="";
			String cnsm ="";
			hssfrow = hssfsheet.getRow(2);
			if (hssfrow != null) {
				if (hssfrow.getCell((short) 0) == null) {
					hbaqqksm = "";
				} else if (hssfrow.getCell((short) 0).getCellType() == 0) {
					hbaqqksm = new Double(hssfrow.getCell((short) 0).getNumericCellValue()).toString();
				} else {// 如果EXCEL表格中的数据类型为字符串型
					hbaqqksm = hssfrow.getCell((short) 0).getStringCellValue().trim();
				}
			}
			hssfrow = hssfsheet.getRow(4);
			if (hssfrow != null) {
				if (hssfrow.getCell((short) 0) == null) {
					sczyqksm = "";
				} else if (hssfrow.getCell((short) 0).getCellType() == 0) {
					sczyqksm = new Double(hssfrow.getCell((short) 0).getNumericCellValue()).toString();
				} else {// 如果EXCEL表格中的数据类型为字符串型
					sczyqksm = hssfrow.getCell((short) 0).getStringCellValue().trim();
				}
			}
			hssfrow = hssfsheet.getRow(6);
			if (hssfrow != null) {
				if (hssfrow.getCell((short) 0) == null) {
					fwzlqksm = "";
				} else if (hssfrow.getCell((short) 0).getCellType() == 0) {
					fwzlqksm = new Double(hssfrow.getCell((short) 0).getNumericCellValue()).toString();
				} else {// 如果EXCEL表格中的数据类型为字符串型
					fwzlqksm = hssfrow.getCell((short) 0).getStringCellValue().trim();
				}
			}
			hssfrow = hssfsheet.getRow(8);
			if (hssfrow != null) {
				if (hssfrow.getCell((short) 0) == null) {
					ylqksm = "";
				} else if (hssfrow.getCell((short) 0).getCellType() == 0) {
					ylqksm = new Double(hssfrow.getCell((short) 0).getNumericCellValue()).toString();
				} else {// 如果EXCEL表格中的数据类型为字符串型
					ylqksm = hssfrow.getCell((short) 0).getStringCellValue().trim();
				}
			}
			hssfrow = hssfsheet.getRow(10);
			if (hssfrow != null) {
				if (hssfrow.getCell((short) 0) == null) {
					cnsm = "";
				} else if (hssfrow.getCell((short) 0).getCellType() == 0) {
					cnsm = new Double(hssfrow.getCell((short) 0).getNumericCellValue()).toString();
				} else {// 如果EXCEL表格中的数据类型为字符串型
					cnsm = hssfrow.getCell((short) 0).getStringCellValue().trim();
				}
			}
			if(hbaqqksm.length()>0 || sczyqksm.length()>0 || fwzlqksm.length()>0 || ylqksm.length()>0 || cnsm.length()>0){
				YwHwZxqksm ywHwZxqksm = new YwHwZxqksm();
				ywHwZxqksm.setYwid(ywid);
				ywHwZxqksm.setCnsm(cnsm);
				ywHwZxqksm.setFwzlqksm(fwzlqksm);
				ywHwZxqksm.setHbaqqksm(hbaqqksm);
				ywHwZxqksm.setQyid(qyid);
				ywHwZxqksm.setSczyqksm(sczyqksm);
				ywHwZxqksm.setTplcode(templateCode);
				ywHwZxqksm.setYlqksm(ylqksm);
				this.SaveOrUpdate(ywHwZxqksm);
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
}
